package cn.rulian.base.basis.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.rulian.base.basis.bo.CommonProperty;
import cn.rulian.base.basis.service.CommonPropertyService;
import cn.rulian.base.basis.vo.CommonPropertyVo;
import cn.rulian.base.common.dto.Message;
import cn.rulian.base.common.util.xx;
import cn.rulian.base.common.vo.Result;

/**
 * 公共参数
 */

@Controller
@RequestMapping("/base/basis/commonProperty")
public class CommonPropertyController {
	
	@Autowired
	private CommonPropertyService service;
	
	//private final Logger log = LoggerFactory.getLogger(getClass());
	
	private final String path="base/basis/commonProperty_"; //页面基本路径

	/**
	 * 页面基本路径
	 */
	private String getPath()
	{
		return path;
	}
	
	
	/**
	 * 列表页面
	 */
	@RequestMapping("/listPage")
	public String listPage() {
		return this.getPath()+"list";
	}

	/**
	 * 查询
	 */
	@RequestMapping("/listData")
	@ResponseBody
	public Result listData(CommonPropertyVo vo) {
		return service.query(vo);
	}


	/**
	 * 增加页面
	 */
	@RequestMapping("/addPage")
	public String addPage() {
		return this.getPath()+"add";
	}

	/**
	 * 增加
	 */
	@RequestMapping("/add")
	@ResponseBody
	public Message add(CommonPropertyVo vo) {
		Message rm = new Message();
		CommonProperty bo = vo.getObj();
		
		try {
			service.save(bo);
			rm.setMsg("保存成功！");
			rm.setCode(Message._SUCCESS);
		} catch (Exception e) {
			rm.setMsg("保存失败，异常信息：<br/>" + e.toString());
			rm.setCode(Message._ERROR);
			e.printStackTrace();
		}
		return rm;
	}

	/**
	 * 修改页面
	 */
	@RequestMapping("/updatePage")
	public String updatePage(Model model, Long id) {
		model.addAttribute("bo", service.findOne(id));
		return this.getPath()+"update";
	}

	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@ResponseBody
	public Message update(CommonPropertyVo vo,Long id) {
		Message rm = new Message();
		try {
			CommonProperty obj = vo.getObj();
			CommonProperty bo = service.findOne(id);
			
			xx.copyObjectField(obj, bo); //属性值copy
			service.save(bo);
			
			rm.setMsg("保存成功！");
			rm.setCode(Message._SUCCESS);
		} catch (Exception e) {
			rm.setMsg("保存失败，异常信息：<br/>" + e.toString());
			rm.setCode(Message._ERROR);
			e.printStackTrace();
		}
		return rm;
	}

	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@ResponseBody
	public Message delete(Long ids[]) {
		Message rm = new Message();
		try {
			for(Long id:ids)
			{
				service.delete(id);
			}
			
			rm.setMsg("删除成功！");
			rm.setCode(Message._SUCCESS);
		} catch (Exception e) {
			rm.setMsg("删除失败，异常信息：<br/>" + e.toString());
			rm.setCode(Message._ERROR);
			e.printStackTrace();
		}
		return rm;
	}
	
	/**
	 * 详情页面
	 */
	@RequestMapping("/detailPage")
	public String detailPage(Model model, Long id) {
		model.addAttribute("bo", service.findOne(id));
		return this.getPath()+"detail";
	}

	/**
	 * 配置页面
	 */
	@RequestMapping("/setPage")
	public String setPage(Model model,String system) {
		model.addAttribute("system", system);
		return this.getPath()+"set";
	}

	/**
	 * 获取配置数据
	 */
	@RequestMapping("/getSetData")
	@ResponseBody
	public Result getSetData(String system) {
		
		CommonPropertyVo vo = new CommonPropertyVo(); //构建查询对象
		vo.setRows(999); //分页大小
		vo.setPage(1);
		vo.setSystem(system); //系统编号
		return service.query(vo);  //查询
	}
	
	@RequestMapping("/setOneData")
	@ResponseBody
	public Message setOneData(Long id,String value) {
		Message rm = new Message();
		try {
			CommonProperty bo = service.findOne(id);
			if(bo!=null)
			{
				bo.setValue(value);
				service.save(bo);
			}
			rm.setMsg("保存成功！");
			rm.setCode(Message._SUCCESS);
		} catch (Exception e) {
			rm.setMsg("保存失败，异常信息：<br/>" + e.toString());
			rm.setCode(Message._ERROR);
			e.printStackTrace();
		}
		return rm;
	}
	
}
